package com.itxz.utils;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * SAX解析工具类
 *
 * @author ldt
 * @date 2019/12/3 10:59
 */
public class SAXUtils {

    private static SAXParserFactory saxpf;

    static {

        saxpf = SAXParserFactory.newInstance();

        saxpf.setNamespaceAware(true);
        try {
            // 不需要验证文件（dtd）
            saxpf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取SAX解析器
     * @return
     */
    public static SAXParser newSAXParser(){
        try {
            SAXParser parser = saxpf.newSAXParser();
            return parser;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
